package com.iyao_app.mvp.password;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.iyao_app.R;
import com.iyao_app.base.BaseFragment;
import com.iyao_app.entry.ResultModel;
import com.iyao_app.http.NetCallBack;
import com.iyao_app.http.NetUtils;
import com.iyao_app.util.Contants;
import com.iyao_app.util.ResourcesUtils;
import com.iyao_app.view.LetterSpacingTextView;
import com.iyaowoqu.base_library.util.KeyboardUtils;
import com.iyaowoqu.base_library.util.RegexUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 作者 :  胡家兵
 * 时间 :  2017/11/14.
 */

public class ForgetPasswordFragment extends BaseFragment {
    @BindView(R.id.et_login_account)
    EditText mEtLoginAccount;
    @BindView(R.id.et_message)
    EditText mEtMessage;
    @BindView(R.id.tv_fail)
    TextView mTvFail;
    Unbinder unbinder;
    @BindView(R.id.tv_send)
    LetterSpacingTextView mTvSend;
    private boolean isSend = false;
    private CountDownTimer mTimer;

    @Override
    public View initRootView(LayoutInflater inflater) {

        rootView = inflater.inflate(R.layout.fragment_forget_password, null);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }


    @Override
    public void setViews() {
        mEtLoginAccount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (mTvFail.getVisibility() == View.VISIBLE) {
                    mTvFail.setVisibility(View.INVISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        mEtMessage.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (mTvFail.getVisibility() == View.VISIBLE) {
                    mTvFail.setVisibility(View.INVISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        mEtMessage.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId,
                                          KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    KeyboardUtils.hideSoftInput(mEtMessage);
                }
                return false;
            }
        });
    }

    @Override
    public void initData() {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        if (mTimer != null)
            mTimer.cancel();
    }

    String str = null;

    private void check() {

        if (TextUtils.isEmpty(mEtLoginAccount.getText().toString().trim()) || mEtLoginAccount.getText().toString().trim().length() < 11 || !RegexUtils.isMobileExact(mEtLoginAccount.getText().toString().trim())) {
            str = ResourcesUtils.getString(R.string.phone_error);
            Animation shake = AnimationUtils.loadAnimation(getActivity(), R.anim.shake);
            mTvFail.setVisibility(View.VISIBLE);
            mTvFail.startAnimation(shake);
            mTvFail.setText(str);
            return;
        }
        if (TextUtils.isEmpty(mEtMessage.getText().toString().trim())) {
            str = ResourcesUtils.getString(R.string.message_code_error);
            Animation shake = AnimationUtils.loadAnimation(getActivity(), R.anim.shake);
            mTvFail.setVisibility(View.VISIBLE);
            mTvFail.startAnimation(shake);
            mTvFail.setText(str);
            return;
        }
        NetUtils.getInstance().check(mEtLoginAccount.getText().toString().trim(), mEtMessage.getText().toString().trim(), new NetCallBack() {
            @Override
            public void onSuccess(String message, int code, String response, ResultModel model) {
                if (code == Contants.SUCCESS_CODE) {
                    Bundle bundle = new Bundle();
                    bundle.putString(Contants.TYPE, mEtLoginAccount.getText().toString().trim());
                    bundle.putString(Contants.OTHER_FIELD, mEtMessage.getText().toString().trim());
                    ((ForgetActivity) getActivity()).changeFragment(bundle);
                } else {
                    if (!TextUtils.isEmpty(message)) {
                        str = ResourcesUtils.getString(R.string.message_code_error);
                        Animation shake = AnimationUtils.loadAnimation(getActivity(), R.anim.shake);
                        mTvFail.setVisibility(View.VISIBLE);
                        mTvFail.startAnimation(shake);
                        mTvFail.setText(str);
                    }
                }
            }

            @Override
            public void onFail(String message, int code) {

            }
        }, null);

    }

    private void senMessage() {
        String str = null;
        if (TextUtils.isEmpty(mEtLoginAccount.getText().toString().trim()) || mEtLoginAccount.getText().toString().trim().length() < 11 || !RegexUtils.isMobileExact(mEtLoginAccount.getText().toString().trim())) {
            str = ResourcesUtils.getString(R.string.phone_error);
            Animation shake = AnimationUtils.loadAnimation(getActivity(), R.anim.shake);
            mTvFail.setVisibility(View.VISIBLE);
            mTvFail.startAnimation(shake);
            mTvFail.setText(str);
            return;
        }
        if (isSend) {
            Animation shake = AnimationUtils.loadAnimation(getActivity(), R.anim.shake);
            mTvFail.setVisibility(View.VISIBLE);
            mTvFail.startAnimation(shake);
            mTvFail.setText(R.string.message_send);
            return;
        }
        NetUtils.getInstance().sms(mEtLoginAccount.getText().toString().trim(), new NetCallBack() {
            @Override
            public void onSuccess(String message, final int code, String response, ResultModel model) {
                if (code == Contants.SUCCESS_CODE) {
                    isSend = true;
                    startTimer();
                } else {
                    isSend = false;
                }
            }

            @Override
            public void onFail(String message, int code) {

            }
        }, null);
    }

    private void startTimer() {
        mTimer = new CountDownTimer(60000, 1000) {

            @Override
            public void onTick(long millisUntilFinished) {
                mTvSend.setText(millisUntilFinished / 1000 + "s后重新获取");
            }

            @Override
            public void onFinish() {
                isSend = false;
                mTvSend.setText(ResourcesUtils.getString(R.string.send_message_code));
                mTvSend.setClickable(true);
            }
        };
        mTimer.start();
    }

    @OnClick({R.id.tv_send, R.id.tv_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_send:
                senMessage();
                break;
            case R.id.tv_next:
                check();
                break;
        }
    }
}
